package org.yihang.common.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;

public class InjectArgumentResolver implements WebArgumentResolver {

	@Autowired
	private ApplicationContext context;

	@Override
	public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
		Inject inject = methodParameter.getParameterAnnotation(Inject.class);
		if (inject != null) {
			if (inject.value().equals("")) {
				Class<?> type = methodParameter.getParameterType();
				return context.getBean(type);
			} else {
				return context.getBean(inject.value());
			}
		}

		return WebArgumentResolver.UNRESOLVED;
	}

}
